<?php  if (! defined('BASEPATH')) exit('No direct script access allowed');
class lienhe extends CI_Controller
{
	public function __construct()
	{
        parent::__construct();	
		$this->pre_message = "";
        $this->load->model('lienhe_model','lienhe');
        $this->load->library('editor_library');
		$this->load->helper('date');
		$this->load->helper('str');
	}
	
	function index()
	{
		$data['title'] = 'Khách hàng liên hệ';
		//$data['add'] = 'lienhe/addnew';
		$config['base_url'] = base_url().'lienhe/index/'.(int)$this->uri->segment(3);  
		$data['num'] = $this->lienhe->count_list();
		$config['total_rows']   =  $data['num'];
		$config['per_page']  =   '20' ;
		$config['uri_segment'] = 4 ;   
		$this->pagination->initialize($config);   
		$data['list'] =   $this->lienhe->get_list($config['per_page'],(int)$this->uri->segment('4'));
		$data['pagination']    = $this->pagination->create_links();            
		$this->_templates['page'] = 'lienhe/index';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function view()
	{            
		$id = (int)$this->uri->segment(3);                              
		$data['rs'] = $this->lienhe->getlienhe($id);
		$data['title'] = 'Xem thông tin liên hệ từ '.$data['rs']->lb_cusmail;
		
		//Cập nhật trang thái
		$id = (int)$this->uri->segment(3);    
		$this->lienhe->set_view($id);
		 
		$this->_templates['page'] = 'lienhe/view';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function addnew()
	{
	  $data['title'] = 'Thêm lienhe mới';
	
	  $this->form_validation->set_rules('subject_vn','Tiêu đề','trim|required');
	  $this->form_validation->set_rules('ks_image','','');
	  $this->form_validation->set_rules('subcontent_vn','','');
	  $this->form_validation->set_rules('content_vn','','');
	  if($this->form_validation->run()== FALSE){
		  $this->pre_message = validation_errors();
	  }else{
		  if($this->lienhe->save()){
			 if($this->input->post('ks_image')!=''){
				 if(file_exists('../uploads/images/'.$this->input->post('ks_image'))){
					 unlink('../uploads/images/'.$this->input->post('ks_image'));
				 }
				//fn_resize_ks_image('../uploads/images/'.$this->input->post('ks_image'),'../uploads/lienhe/'.$this->input->post('ks_image'),160,120); 
			  }                      
			  $this->session->set_flashdata('message','Lưu thành công');
			  redirect('lienhe/listlienhe');
		  }
	  }
	
	  $data['message'] = $this->pre_message;
	  $this->_templates['page'] = 'lienhe/add';
	  $this->site->load($this->_templates['page'],$data);
	}

	function update()
	{            
		$id = (int)$this->uri->segment(3);                              
		$data['rs'] = $this->lienhe->getlienhe($id);
		$data['title'] = 'Cập nhật: '.$data['rs']->subject_vn;
		
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('subject_vn','Tên chủ đề','trim|required');
		
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}else{
				if($this->lienhe->save())
				{
					$this->session->set_flashdata('message','Cập nhật chuyên mục thành công');
					redirect('lienhe');
				}
		 	}
		}  
		$this->_templates['page'] = 'lienhe/update';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function del()
	{
		$id = (int)$this->uri->segment(3); 
		$this->lienhe->delete($id);
		redirect("lienhe");
	}
	
	function delslienhe()
	{
		if(!empty($_POST['ar_id'])){
			$arr_id = $this->input->post('ar_id');
			if(!empty($_POST['btn_submit'])){
				for($i=0;$i<sizeof($arr_id);$i++){
					if($arr_id[$i]){
						if($this->lienhe->delete($arr_id[$i])){
							$this->session->set_flashdata('message','Xóa Thành Công');
						}else{
							$this->session->set_flashdata('message','Xóa Không Thành Công');
						}
					}
				}
			}
		}
		redirect('lienhe/index');
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */